package com.lsc.admin.demo.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lsc.admin.demo.api.UserService;
import com.lsc.admin.demo.bo.UserBO;
import com.lsc.admin.demo.entity.User;
import com.smf.lsc.core.exception.BaseException;
import com.smf.lsc.core.respones.Result;
import com.smf.lsc.core.respones.ResultUtil;
import com.smf.lsc.security.util.AuthUserUtil;

import io.seata.spring.annotation.GlobalTransactional;

/**
 * providerServer
 * @author ln
 *
 */
@RestController
@Service("userService")
public class UserServiceImpl implements UserService {
	
	@Autowired
	UserBO userBO;
	
	/**
	 * @test 微服务调用
	 * @return
	 * @throws BaseException
	 */
	@Override
	@GetMapping("/demo/remoteServer")
	public Result<String> getFegin()
	{
		AuthUserUtil.getCurrentUser().getAccount();
		userBO.selectAll();
		return ResultUtil.successResult("远程服务调用成功!-【admin返回】", "");
	}
	
	@Override
	@GetMapping("/demo/getCurrentUser")
	public Result<String> getCurrentUser()
	{
		String currentUserJson=JSON.toJSONString(AuthUserUtil.getCurrentUser());
		System.out.println("当前登录用户:"+currentUserJson);
		return ResultUtil.successResult(currentUserJson, "");
	}

	@Transactional
	@Override
	public boolean save(User user) {
		// 操作1
//		User user1 = new User();
//		user1.setId(String.valueOf(Long.valueOf(user.getId())+1));
////		user1.setId(String.valueOf(Long.valueOf(user.getId())));
//		user1.setUserName(user.getUserName()+"操作1");
//		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");// 设置日期格式
//		user1.setUserPassword(sdf.format(new Date()));
//		boolean save = userBO.save(user1);
		//操作2
		return userBO.save(user);
	}

	@Override
	public List<User> list() {
		// TODO Auto-generated method stub
		return userBO.selectAll();
	}

	@Override
	public IPage<User> selectPage(IPage<User> page, Wrapper<User> queryWrapper) {
		// TODO Auto-generated method stub
		return userBO.getBaseMapper().selectPage(page, queryWrapper);
	}

	@Transactional
	@Override
	public Result<String> dtpInsertUser() {
		User user1 = new User();
		user1.setId("111111");
		user1.setUserName("操作1");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");// 设置日期格式
		user1.setUserPassword(sdf.format(new Date()));
		userBO.save(user1);
		return ResultUtil.successResult("保存成功", "");
	}
}